﻿using backend.Entity;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.StaticFiles;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace backend.Controllers
{
    [Authorize]
    [Route("api/[controller]")]
    [ApiController]
    public class FileController : Controller
    {
        private readonly MyContext _context;
        public FileController(MyContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 文件
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("[action]/{id}")]
        [AllowAnonymous]
        public IActionResult Show([BindRequired] int id)
        {
            FileInfo file = _context.FileInfos.FirstOrDefault(a => a.Id == id);
            string contentType;
            new FileExtensionContentTypeProvider().TryGetContentType(file.Path, out contentType);
            return PhysicalFile(file.Path, contentType ?? "application/octet-stream");
        }
    }
}
